package com.windgo.service;

import com.github.pagehelper.PageHelper;
import com.windgo.bean.*;
import com.windgo.bean.bo.wxgoods.WxMarketGoodsBo;
import com.windgo.bean.bo.wxhomebo.*;
import com.windgo.bean.vo.WxHomeAboutVO;
import com.windgo.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author wuwen
 * @since 2022/10/15
 * 17:16
 */
@Service
public class WxHomeServiceImpl implements WxHomeService {
    @Autowired
    MarketAdMapper marketAdMapper;
    @Autowired
    MarketBrandMapper marketBrandMapper;
    @Autowired
    MarketCategoryMapper marketCategoryMapper;
    @Autowired
    MarketCouponMapper marketCouponMapper;
    @Autowired
    MarketGoodsMapper marketGoodsMapper;
    @Autowired
    MarketTopicMapper marketTopicMapper;
    @Autowired
    MarketSystemMapper marketSystemMapper;

    @Override
    public WxHomeBo search() {
        WxHomeBo wxHomeBo = new WxHomeBo();
        //首先查Market_ad
        List<WxBannerBo> wxBannerBos = marketAdMapper.selectAll();
        wxHomeBo.setBanner(wxBannerBos);

        List<Integer> marketGoods2 = marketGoodsMapper.searchBrandIdIsNoZero();
        ArrayList<WxBrandBo> wxBrandBos1 = new ArrayList<>();
        for (Integer integer : marketGoods2) {
            WxBrandBo wxBrandBo = marketBrandMapper.searchByBrandId(integer);
            wxBrandBos1.add(wxBrandBo);
        }

        wxHomeBo.setBrandList(wxBrandBos1);

        ArrayList<WxChannelBo> wxCategoryBos1 = new ArrayList<>();
        List<WxChannelBo> wxChannelBos = marketCategoryMapper.selectAll();
        for (WxChannelBo wxChannelBo : wxChannelBos) {
            Integer id = wxChannelBo.getId();
            MarketCategory marketCategory = marketCategoryMapper.selectByPrimaryKey(id);
            if(marketCategory.getPid()==0){
             wxCategoryBos1.add(wxChannelBo);
            }
        }
        wxHomeBo.setChannel(wxCategoryBos1);
        List<WxCouponBo> wxCouponBos = marketCouponMapper.selectAll();
        ArrayList<WxCouponBo> list = new ArrayList<>();
        for (WxCouponBo wxCouponBo : wxCouponBos) {
            Integer id = wxCouponBo.getId();
            MarketCoupon marketCoupon = marketCouponMapper.selectByPrimaryKey(id);
            if(marketCoupon.getType()==0){
                list.add(wxCouponBo);
            }
        }
        //先查一级类目，获取到一个一级类目的list.
        wxHomeBo.setCouponList(list);
        ArrayList<WxFloorGoodsBo> wxFloorGoodsBos = new ArrayList<>();
        MarketCategoryExample marketCategoryExample = new MarketCategoryExample();
        MarketCategoryExample.Criteria criteria2 = marketCategoryExample.createCriteria();
        criteria2.andPidEqualTo(0);
        List<MarketCategory> marketCategories = marketCategoryMapper.selectByExample(marketCategoryExample);
        for (MarketCategory marketCategory : marketCategories) {
            WxFloorGoodsBo wxFloorGoodsBo = new WxFloorGoodsBo();
            MarketCategoryExample marketCategoryExample1 = new MarketCategoryExample();
            MarketCategoryExample.Criteria criteria = marketCategoryExample1.createCriteria();
            marketCategoryExample1.setDistinct(true);
            criteria.andPidEqualTo(marketCategory.getId());
            List<MarketCategory> marketCategories1 = marketCategoryMapper.selectByExample(marketCategoryExample1);
            ArrayList<WxGoodsBo> wxGoodsBos = new ArrayList<>();
            int i=0;
            for (MarketCategory category : marketCategories1) {
                List<WxGoodsBo> wxGoodsBos1 = marketGoodsMapper.searchById(category.getId());
                for (WxGoodsBo wxGoodsBo : wxGoodsBos1) {
                    wxGoodsBos.add(wxGoodsBo);
                    if(wxGoodsBos.size()>5){
                        break;
                    }
                }
            }
            wxFloorGoodsBo.setId(marketCategory.getId());
            wxFloorGoodsBo.setName(marketCategory.getName());
            wxFloorGoodsBo.setGoodsList(wxGoodsBos);
            wxFloorGoodsBos.add(wxFloorGoodsBo);
        }
        wxHomeBo.setFloorGoodsList(wxFloorGoodsBos);
        String sort="update_time";
        String order="desc";
       // PageHelper.startPage(page,limit);
        MarketGoodsExample marketGoodsExample = new MarketGoodsExample();
        MarketGoodsExample.Criteria criteria = marketGoodsExample.createCriteria();
        criteria.andIsNewEqualTo(true);
        marketGoodsExample.setOrderByClause(sort+" "+order);
        List<MarketGoods> marketGoods = marketGoodsMapper.selectByExample(marketGoodsExample);
        ArrayList<WxNewGoodsBo> wxNewGoodsBos = new ArrayList<>();
        for (MarketGoods marketGood : marketGoods) {
            WxNewGoodsBo wxNewGoodsBo = new WxNewGoodsBo();
            wxNewGoodsBo.setBrief(marketGood.getBrief());
            wxNewGoodsBo.setCounterPrice(marketGood.getCounterPrice());
            wxNewGoodsBo.setId(marketGood.getId());
            wxNewGoodsBo.setIsHot(marketGood.getIsHot());
            wxNewGoodsBo.setIsNew(marketGood.getIsNew());
            wxNewGoodsBo.setName(marketGood.getName());
            wxNewGoodsBo.setPicUrl(marketGood.getPicUrl());
            wxNewGoodsBo.setRetailPrice(marketGood.getRetailPrice());
            wxNewGoodsBos.add(wxNewGoodsBo);
            if(wxNewGoodsBos.size()>=6){
                break;
            }
        }
        wxHomeBo.setNewGoodsList(wxNewGoodsBos);
        MarketGoodsExample marketGoodsExample1 = new MarketGoodsExample();
        MarketGoodsExample.Criteria criteria1 = marketGoodsExample1.createCriteria();
        criteria1.andIsHotEqualTo(true);
        marketGoodsExample1.setOrderByClause(sort+" "+order);
        List<MarketGoods> marketGoods1 = marketGoodsMapper.selectByExample(marketGoodsExample1);
        ArrayList<WxHotGoodsBo> wxHotGoodsBos = new ArrayList<>();
        for (MarketGoods goods : marketGoods1) {
            WxHotGoodsBo wxHotGoodsBo = new WxHotGoodsBo();
            wxHotGoodsBo.setBrief(goods.getBrief());
            wxHotGoodsBo.setCounterPrice(goods.getCounterPrice());
            wxHotGoodsBo.setId(goods.getId());
            wxHotGoodsBo.setIsHot(goods.getIsHot());
            wxHotGoodsBo.setIsNew(goods.getIsNew());
            wxHotGoodsBo.setName(goods.getName());
            wxHotGoodsBo.setPicUrl(goods.getPicUrl());
            wxHotGoodsBo.setRetailPrice(goods.getRetailPrice());
            wxHotGoodsBos.add(wxHotGoodsBo);
            if(wxHotGoodsBos.size()>=6){
                break;
            }
        }
        wxHomeBo.setHotGoodsList(wxHotGoodsBos);
        MarketTopicExample marketTopicExample = new MarketTopicExample();
        marketTopicExample.setOrderByClause(sort+" "+order);
        List<MarketTopic> marketTopics = marketTopicMapper.selectByExample(marketTopicExample);
        ArrayList<WxTopicGoodsBo> wxTopicGoodsBos = new ArrayList<>();
        for (MarketTopic marketTopic : marketTopics) {
            WxTopicGoodsBo wxTopicGoodsBo = new WxTopicGoodsBo();
            wxTopicGoodsBo.setId(marketTopic.getId());
            wxTopicGoodsBo.setPicUrl(marketTopic.getPicUrl());
            wxTopicGoodsBo.setPrice(marketTopic.getPrice());
            wxTopicGoodsBo.setReadCount(marketTopic.getReadCount());
            wxTopicGoodsBo.setSubtitle(marketTopic.getSubtitle());
            wxTopicGoodsBo.setTitle(marketTopic.getTitle());
            wxTopicGoodsBos.add(wxTopicGoodsBo);
            if(wxTopicGoodsBos.size()>=6){
                break;
            }
        }
        wxHomeBo.setTopicList(wxTopicGoodsBos);
        return wxHomeBo;
    }

    @Override
    public WxHomeAboutVO aboutHome() {
        WxHomeAboutVO wxHomeAboutVO = new WxHomeAboutVO();
        List<MarketSystem> marketSystemList = marketSystemMapper.selectAllInfoFromSystemTable();
        for (MarketSystem marketSystem : marketSystemList) {
            if (marketSystem.getKeyName().equals("market_mall_address")){
                wxHomeAboutVO.setAddress(marketSystem.getKeyValue());
            }else if (marketSystem.getKeyName().equals("market_mall_latitude")){
                wxHomeAboutVO.setLatitude(marketSystem.getKeyValue());
            }else if (marketSystem.getKeyName().equals("market_mall_longitude")){
                wxHomeAboutVO.setLongitude(marketSystem.getKeyValue());
            }else if (marketSystem.getKeyName().equals("market_mall_name")){
                wxHomeAboutVO.setName(marketSystem.getKeyValue());
            }else if (marketSystem.getKeyName().equals("market_mall_phone")){
                wxHomeAboutVO.setPhone(marketSystem.getKeyValue());
            }else if (marketSystem.getKeyName().equals("market_mall_qq")){
                wxHomeAboutVO.setQq(marketSystem.getKeyValue());
            }
        }
        return wxHomeAboutVO;
    }
}

